import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { UserModule } from './user/user.module';
import { User } from './user/entities/user.entity';
import { Role } from './user/entities/role.entity';
import { Permission } from './user/entities/permission.entity';
import { JwtModule } from '@nestjs/jwt';
import { RedisModule } from './redis/redis.module';
import { EmailModule } from './email/email.module';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { LoginGuard } from './login.guard';
import { PermissionGuard } from './permission.guard';
import { MeetingRoomModule } from './meeting-room/meeting-room.module';
import { MeetingRoom } from './meeting-room/entities/meeting-room.entity';
import { BookingModule } from './booking/booking.module';
import { Booking } from './booking/entities/booking.entity';



@Module({
  imports: [
    JwtModule.registerAsync({
      global: true,
      useFactory(configService:ConfigService){
        return {
          secret: configService.get('jwt_secret'),
          signOptions: { expiresIn: '30m' }
        }
      },
      inject: [ConfigService]
    }),
    TypeOrmModule.forRootAsync({  //需要读取文件或者远程读取配置时使用
      useFactory(configService: ConfigService) {  
            return {
              type: "mysql",
              host: configService.get('mysql_server_host'),
              port: configService.get('mysql_server_port'),
              username: configService.get('mysql_server_username'),
              password: configService.get('mysql_server_password'),
              database: configService.get('mysql_server_database'),
              synchronize: true,
              logging: true,
              entities: [User, Role, Permission, MeetingRoom, Booking],
              poolSize: 10,
              connectorPackage: 'mysql2',
              extra: {
                authPlugin: 'sha256_password',
              }
            }
      },
      inject: [ConfigService]
    }),
    ConfigModule.forRoot({  //
      isGlobal: true,
      envFilePath: 'src/.env'
    }),
    UserModule, RedisModule, EmailModule, MeetingRoomModule, BookingModule,
  ],
  controllers: [AppController],
  providers: [
    AppService,
    {
      provide: 'APP_GUARD',
      useClass: LoginGuard
    },
    {
      provide: 'APP_GUARD',
      useClass: PermissionGuard
    }
  ],
})
export class AppModule { }
